;;; yp-make.el --- help build project in Emacs
;;
;; Copyright (c) 
;;
;;
;; This file is a lisp packet used in emacs
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; 
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;; 
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;

;; This program is used to maintain the compile command of your project.
;; Emacs provide a default sulotion for compile, that is the command 'compile'
;; When you type M-x compile, it will run a compile command on the directory of
;; current buffer. But for a large project, Makefile may not on the same directory
;; with the current buffer you edit. So I write this program to run the compile
;; command regardless the current directory.

;; You should add your Makefile path to the variable yp-make-dir, and your make command
;; to yp-make-command (maybe just a 'make' or 'make -i'...). If you have more than one
;; project, you could modify yp-make-select to select which project you want to compile.
;; You can run the command yp-make-execute to compile your project, every time you modify
;; yp-make-select or yp-make-do-select, you can run the command yp-make-save-and-execute
;; to let it become effective.

;; I should say it is not a normative emacs extension, because you always need to modify this
;; file yourself, but it is easy to write and easy to use. If have any problem, you can send
;; email to yupeng820921@gmail.com.

;; vertion 0.1

(defvar yp-make-select nil)
(defvar yp-make-timer nil)
(defvar yp-make-buffer "*compilation*")

;; modify the number for yourself to select which project you want to build.
(setq yp-make-select 3)

;; you should modify the below function yourself, add your own porject path and compile command
(defun yp-make-do-select ()
  (cond
   ((= 0 yp-make-select)
    (setq yp-make-dir "~/lisp/cedet/Makefile")
    (setq yp-make-command "make"))
   ((= 1 yp-make-select)
    (setq yp-make-dir "/home/yupeng/work/arm-linux/android_work/egk/kernel/Makefile")
    (setq yp-make-command "./b4.sh"))
   ((= 2 yp-make-select)
    (setq yp-make-dir "/home/yupeng/work/arm-linux/android_work/urbetter/kernel/Makefile")
    (setq yp-make-command "./build.sh"))
   ((= 3 yp-make-select)
    (setq yp-make-dir "/home/yupeng/work/arm-linux/android_work/pollux/kernel/Makefile")
    (setq yp-make-command "./build.sh"))
   ((= 4 yp-make-select)
    (setq yp-make-dir "/home/yupeng/work/arm-linux/android_work/egk/cupcake_r3/external/alsa_sound/Android.mk")
    (setq yp-make-command ". ../../build/envsetup.sh ; mm"))
  )
)

(yp-make-do-select)

;; export
(defun yp-make-execute ()
  "save current buffe, then run 'compile', 
the difault dir and command pass to compile 
is defined in 'yp-make-do-select'"
  (interactive)
  (save-buffer (current-buffer))
  (let (buffer)
    (setq buffer (find-file-noselect yp-make-dir))
    (set-buffer buffer)
    (compile yp-make-command)
    (kill-buffer buffer)))

(defun yp-make-save-and-execute ()
  "save and eval the yp-make.el file, then run 'compile',
when it is called, the current buffer should be 'yp-make.el'.
It is used to add or remove a project"
  (interactive)
  (save-buffer (current-buffer))
  (eval-current-buffer)
  (yp-make-execute))

(defun yp-make-add-select (num)
  "move the current porject to next"
  (interactive "P")
  (unless num
    (setq num 1))
  (setq yp-make-select (+ yp-make-select num))
  (yp-make-do-select)
  (yp-make-execute))

(defun yp-make-dec-select (num)
  "move the current project to next"
  (interactive "P")
  (unless num
    (setq num 1))
  (setq yp-make-select (- yp-make-select num))
  (yp-make-do-select)
  (yp-make-execute))

(defun yp-make-show ()
  "show the current project number"
  (interactive)
  (message "yp-make-select: %d  yp-make-dir: %s" yp-make-select yp-make-dir))

(provide 'yp-make)
